package com.floreantpos.bo.ui.explorer;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

import com.floreantpos.POSConstants;
import com.floreantpos.main.Application;
import com.floreantpos.model.Store;
import com.floreantpos.model.dao.StoreDAO;
import com.floreantpos.swing.MessageDialog;
import com.floreantpos.swing.TransparentPanel;
import com.floreantpos.ui.PosTableRenderer;
import com.floreantpos.ui.dialog.BeanEditorDialog;
import com.floreantpos.ui.dialog.ConfirmDeleteDialog;
import com.floreantpos.ui.model.StoreForm;

public class StoreExplorer extends TransparentPanel {
	private List<Store> storeList;
	
	private JTable table;

	private StoreExplorerTableModel tableModel;
	
//	private String role;
	
	public StoreExplorer() {
		this(POSConstants.ADMIN);
	}
	
	public StoreExplorer(String role) {
//		this.role = role;
		this.setName(com.floreantpos.POSConstants.STORE);
		Application.getInstance().setLastCommand(role);
		
		if(role != null && role.equals(POSConstants.ADMIN)){
			StoreDAO dao = new StoreDAO();
			storeList = dao.findAll();
		}else if(role != null && role.equals(POSConstants.SERVER)){
			StoreDAO dao = new StoreDAO();
			storeList = dao.findAllByToday();
		}
		
		tableModel = new StoreExplorerTableModel();
		table = new JTable(tableModel);
		table.setDefaultRenderer(Object.class, new PosTableRenderer());
		
		setLayout(new BorderLayout(5,5));
		add(new JScrollPane(table));
		
		JButton addButton = new JButton(com.floreantpos.POSConstants.ADD);
		addButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					StoreForm editor = new StoreForm();
					
					boolean enableDate = false;
					JFrame frame;
					if(Application.getInstance().getLastCommand() != null 
							&& Application.getInstance().getLastCommand().equals(POSConstants.SERVER)){
						frame = Application.getInstance().getManagementWindow(POSConstants.STORE);
						enableDate = false;
					}else{
						frame = Application.getInstance().getBackOfficeWindow();
						enableDate = true;
					}
					editor.enableModifiedTime(enableDate);
					
					BeanEditorDialog dialog = new BeanEditorDialog(editor, frame, true);
					dialog.open();
					if (dialog.isCanceled())
						return;
					Store store = (Store) editor.getBean();
					tableModel.addStore(store);
				} catch (Exception x) {
					x.printStackTrace();
					MessageDialog.showError(com.floreantpos.POSConstants.ERROR_MESSAGE, x);
				}
			}
			
		});
		
		JButton editButton = new JButton(com.floreantpos.POSConstants.EDIT);
		editButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					int index = table.getSelectedRow();
					if (index < 0)
						return;

					Store store = storeList.get(index);

					StoreForm editor = new StoreForm(store);

					boolean enableDate = false;
					JFrame frame;
					if(Application.getInstance().getLastCommand() != null 
							&& Application.getInstance().getLastCommand().equals(POSConstants.SERVER)){
						frame = Application.getInstance().getManagementWindow(POSConstants.STORE);
						enableDate = false;
					}else{
						frame = Application.getInstance().getBackOfficeWindow();
						enableDate = true;
					}
					editor.enableModifiedTime(enableDate);
					
					BeanEditorDialog dialog = new BeanEditorDialog(editor, frame, true);
					dialog.open();
					if (dialog.isCanceled())
						return;

					table.repaint();
				} catch (Throwable x) {
					MessageDialog.showError(com.floreantpos.POSConstants.ERROR_MESSAGE, x);
				}
			}
			
		});
		JButton deleteButton = new JButton(com.floreantpos.POSConstants.DELETE);
		deleteButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					int index = table.getSelectedRow();
					if (index < 0)
						return;

					if (ConfirmDeleteDialog.showMessage(StoreExplorer.this, com.floreantpos.POSConstants.CONFIRM_DELETE, com.floreantpos.POSConstants.DELETE) == ConfirmDeleteDialog.YES) {
						Store store = storeList.get(index);
						StoreDAO dao = new StoreDAO();
						dao.delete(store);
						tableModel.deleteStore(store, index);
					}
				} catch (Exception x) {
					MessageDialog.showError(com.floreantpos.POSConstants.ERROR_MESSAGE, x);
				}
			}
			
		});

		TransparentPanel panel = new TransparentPanel();
		panel.add(addButton);
		panel.add(editButton);
		if(role != null && role.equals(POSConstants.ADMIN)){
			panel.add(deleteButton);
		}
		add(panel, BorderLayout.SOUTH);
	}
	
	class StoreExplorerTableModel extends AbstractTableModel {
		String[] columnNames = {com.floreantpos.POSConstants.ID, com.floreantpos.POSConstants.NAME, com.floreantpos.POSConstants.STORE_NUMBER, com.floreantpos.POSConstants.DATE, com.floreantpos.POSConstants.USER};
		
		public int getRowCount() {
			if(storeList == null) {
				return 0;
			}
			return storeList.size();
		}

		public int getColumnCount() {
			return columnNames.length;
		}
		
		@Override
		public String getColumnName(int column) {
			return columnNames[column];
		}
		
		@Override
		public boolean isCellEditable(int rowIndex, int columnIndex) {
			return false;
		}

		public Object getValueAt(int rowIndex, int columnIndex) {
			if(storeList == null)
				return "";
			
			Store store = storeList.get(rowIndex);
			
			switch(columnIndex) {
				case 0:
					return String.valueOf(store.getId());
					
				case 1:
					return store.getName();
					
				case 2:
					return store.getNumbers();
					
				case 3:
					return store.getModifiedTime();
					
				case 4:
					return (store.getUser()!=null?store.getUser().getFirstName():"");
			}
			return null;
		}

		public void addStore(Store store) {
			int size = storeList.size();
			storeList.add(store);
			fireTableRowsInserted(size, size);
		}
		
		public void deleteStore(Store store, int index) {
			storeList.remove(store);
			fireTableRowsDeleted(index, index);
		}
	}
}
